package com.ruoyi.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.io.FileUtil;
import com.ruoyi.business.domain.bo.FileAttachmentBo;
import com.ruoyi.business.domain.vo.FileAttachmentVo;
import com.ruoyi.business.service.IFileAttachmentService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringExtUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.ISysOssService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 档案影像
 *
 * @author xxyd
 * @date 2024-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/fileAttachment")
public class FileAttachmentController extends BaseController {

    private final IFileAttachmentService iFileAttachmentService;
    private final ISysOssService iSysOssService;

    /**
     * 查询档案影像列表
     */
    @SaCheckPermission("business:fileAttachment:list")
    @GetMapping("/list")
    public TableDataInfo<FileAttachmentVo> list(FileAttachmentBo bo, PageQuery pageQuery) {
        return iFileAttachmentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出档案影像列表
     */
    @SaCheckPermission("business:fileAttachment:export")
    @Log(title = "档案影像", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FileAttachmentBo bo, HttpServletResponse response) {
        List<FileAttachmentVo> list = iFileAttachmentService.queryList(bo);
        ExcelUtil.exportExcel(list, "档案影像", FileAttachmentVo.class, response);
    }

    /**
     * 获取档案影像详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:fileAttachment:query")
    @GetMapping("/{id}")
    public R<FileAttachmentVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(iFileAttachmentService.queryById(id));
    }

    /**
     * 新增档案影像
     */
    @SaCheckPermission("business:fileAttachment:add")
    @Log(title = "档案影像", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FileAttachmentBo bo) {
        return toAjax(iFileAttachmentService.insertByBo(bo));
    }

    /**
     * 修改档案影像
     */
    @SaCheckPermission("business:fileAttachment:edit")
    @Log(title = "档案影像", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FileAttachmentBo bo) {
        return toAjax(iFileAttachmentService.updateByBo(bo));
    }

    /**
     * 删除档案影像
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:fileAttachment:remove")
    @Log(title = "档案影像", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iFileAttachmentService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @Log(title = "上传影像资料", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<FileAttachmentBo> avatar(@RequestPart("file") MultipartFile file, FileAttachmentBo bo) {
        if (file.isEmpty()) {
            return R.fail("上传图片异常，请联系管理员");
        } else {
            String extension = FileUtil.extName(file.getOriginalFilename());
            if (!StringExtUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
                return R.fail("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
            }
            SysOssVo oss = iSysOssService.upload(file);
            bo.setArchivalImagery(oss.getUrl());
            bo.setName(oss.getOriginalName());
            bo.setArchivalId(oss.getOssId());
            Boolean flag = iFileAttachmentService.insertByBo(bo);
            if (flag) {
                return R.ok(bo);
            } else {
                return R.fail("上传图片异常，请联系管理员");
            }
        }
    }
}
